﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YYS
{
    /// <summary>
    /// 日志实体
    /// </summary>
    public class LogModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public LogModel()
        {
            this.Add("StackTrace", null);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="system">系统</param>
        /// <param name="page">类或页</param>
        /// <param name="method">方法</param>
        /// <param name="title">标题</param>
        public LogModel(string system, string page, string method, string title)
        {
            this.FsL_System = system;
            this.FsL_Page = page;
            this.FsL_Method = method;
            this.FsL_Title = title;
            this.Add("StackTrace", null);
        }

        public int FnL_ID { get; set; }

        string _FsL_Key = Guid.NewGuid().ToString("n");
        /// <summary>
        /// Key
        /// </summary>
        public string FsL_Key
        {
            get { return _FsL_Key; }
            set { _FsL_Key = value; }
        }
        DateTime _FdL_Time = DateTime.Now;
        /// <summary>
        /// 日志时间
        /// </summary>
        public DateTime FdL_Time
        {
            get { return _FdL_Time; }
            set { _FdL_Time = value; }
        }
        /// <summary>
        /// 系统
        /// </summary>
        public string FsL_System { get; set; }
        /// <summary>
        /// 类
        /// </summary>
        public string FsL_Page { get; set; }
        /// <summary>
        /// 方法
        /// </summary>
        public string FsL_Method { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string FsL_Title { get; set; }

        Dictionary<string, object> _Detail = new Dictionary<string, object>();
        /// <summary>
        /// 错误明细
        /// </summary>
        public Dictionary<string, object> Detail
        {
            get { return _Detail; }
            set { _Detail = value; }
        }

        public string FxL_Detail { get; set; }

        public int FnL_IsSystemError { get; set; }

        /// <summary>
        /// 设置FsL_Title
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public LogModel SetTitle(string title)
        {
            this.FsL_Title = title;
            return this;
        }

        /// <summary>
        /// 新增一个明细值
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public LogModel Add(string Key, object Value)
        {
            this.Detail[Key] = Value;
            return this;
        }

        /// <summary>
        /// Key Is SystemError
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public LogModel AddSystemError(object Value)
        {
            Exception _Ex = Value as Exception;
            if (_Ex != null)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic["Source"] = _Ex.Source;
                dic["Message"] = _Ex.Message;
                if (_Ex.StackTrace != null)
                {
                    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("\r\n");
                    dic["StackTraceString"] = reg.Split(_Ex.StackTrace).Select(x => x).ToList();
                }
                dic["ExceptionMethod"] = _Ex.TargetSite;
                this.Detail["SystemError"] = dic;
            }
            else
            {
                this.Detail["SystemError"] = Value;
            }
            this.FnL_IsSystemError = 1;
            return this;
        }

        /// <summary>
        /// 返回json格式
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.jConvertObjectToJson();
        }


    }
}
